//
//  EditAddressView.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/24.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "EditAddressView.h"
#import "GetAeraView.h"
//编辑收货地址
#define EDIT_ADD_URL @"/api/ApiUser/editReceivingAddr"
@interface EditAddressView ()
{
    __weak IBOutlet UITextField *nameText;
    __weak IBOutlet UITextField *phoneText;
    __weak IBOutlet UILabel *addressLab;
    
    __weak IBOutlet UITextView *addressDetailTetx;
    __weak IBOutlet UIButton *commitBtn;
    __weak IBOutlet UIView *bgView;
    __weak IBOutlet UILabel *l1;
    __weak IBOutlet UILabel *l2;
    __weak IBOutlet UILabel *l3;
    __weak IBOutlet UILabel *l4;
    
    //记录
    NSString *_name;
    NSString *_phone;
    NSString *_detail;
    
}
@end
@implementation EditAddressView

-(void)awakeFromNib
{
    bgView.backgroundColor = QBB_GRAY_COLOR;
    commitBtn.layer.cornerRadius = 2;
    commitBtn.layer.masksToBounds = YES;
    commitBtn.backgroundColor = QBB_RED_COLOR;
    CGFloat add = 0;
    [self addSubview:[WYPublic getFenGexian:0 :(CGRectGetMaxY(l1.frame)+add) :WYMWITH :0.5]];
    [self addSubview:[WYPublic getFenGexian:0 :(CGRectGetMaxY(l2.frame)+add) :WYMWITH :0.5]];
    [self addSubview:[WYPublic getFenGexian:0 :(CGRectGetMaxY(l3.frame)+add) :WYMWITH :0.5]];
    [self addSubview:[WYPublic getFenGexian:0 :(CGRectGetMaxY(l4.frame)+add+5) :WYMWITH :0.5]];
}
//改变地址)
- (void)changeArea:(NSString *)area
{
    addressLab.text = area;
    [self removeZheZhao];
}
//添加一个遮罩层
- (void)addZheZhao
{
    UIView *zheZhao = [[UIView alloc]initWithFrame:CGRectMake(0,0,WYMWITH, WYMHEIGHT)];
    zheZhao.tag = 1111;
    zheZhao.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    UIActivityIndicatorView *ac = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    ac.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2);
    [ac startAnimating];
    
    [self.father.view addSubview:zheZhao];
}
//去掉遮罩
- (void)removeZheZhao
{
    UIView *zhezhao = [self.father.view viewWithTag:1111];
    [zhezhao removeFromSuperview];
}
//地址透明按钮的监听
- (IBAction)addressbtnOn:(id)sender {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    [self addZheZhao];
    
    GetAeraView *getv = [[[NSBundle mainBundle] loadNibNamed:@"GetAeraView" owner:self options:nil]lastObject];
//    [getv morenzhi:[nameBtn titleForState:UIControlStateNormal]];
    getv.father =self;
    [self.father.view addSubview:getv];
    getv.frame = CGRectMake(WYMWITH/2 - getv.frame.size.width/2, -1*getv.frame.size.height, getv.frame.size.width, getv.frame.size.height);
    [UIView animateWithDuration:0.15 animations:^{
        getv.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2-100*AUTOY);
    }];
}

//保存按钮的监听事件
- (IBAction)commitBtnOnClick:(id)sender {
     [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if (nameText.text.length == 0) {
        [WYPublic showMessageWithTitle:@"提示" Msg:@"收货人不能为空"];
        return;
    }
    if (phoneText.text.length == 0) {
        [WYPublic showMessageWithTitle:@"提示" Msg:@"联系方式不能为空"];
        return;
    }
    if (addressLab.text.length == 0) {
        [WYPublic showMessageWithTitle:@"提示" Msg:@"地区不能为空"];
        return;
    }
    if (addressDetailTetx.text.length == 0) {
        [WYPublic showMessageWithTitle:@"提示" Msg:@"详细地址不能为空"];
        return;
    }
    if (self.sheng == nil && self.shi == nil &&self.xian == nil&&[_name isEqualToString:nameText.text]&&[_phone isEqualToString:phoneText.text]&&[_detail isEqualToString:addressDetailTetx.text]) {
        return;
    }
    if (self.sheng == nil) {
        self.sheng = @"";
    }
    if (self.shi == nil) {
        self.shi = @"";
    }
    if (self.xian == nil) {
        self.xian = @"";
    }
    
    NSString *name = nameText.text;
    NSString *phone = phoneText.text;
    NSString *xiangxi = addressDetailTetx.text;
    if (name.length == 0) {
        name = @"";
    }
    if (phone.length == 0) {
        phone = @"";
    }
    if (xiangxi.length == 0) {
        xiangxi = @"";
    }
    [WYPublic addZheZhaoToWindowWithTitle:nil];
    NSDictionary *params =  @{@"shouhuoren":name,@"mobile":phone,@"sheng":self.sheng,@"shi":self.shi,@"xian":self.xian,@"xiangxi":xiangxi,@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
    
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,EDIT_ADD_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if (ret.intValue == 0) {
            [WYPublic showMessageWithTitle:@"提示：" Msg:@"保存地址成功"];
            if(self.father.isZhongJiangFlag){
//                [self.father.lastFooter fillAddressWith:nameText.text Phone:phoneText.text Add:[NSString stringWithFormat:@"%@%@",addressLab.text,addressDetailTetx.text] Andtime:dic[@"data"][@"status_time"]];
                [self.father.navigationController popViewControllerAnimated:YES];
            }else{
                _name = nameText.text;
                _phone = phoneText.text;
                _detail = addressDetailTetx.text;
            }
        }else{
            if(ret.intValue>2000 && ret.intValue < 3000){
                //用户过期，强制重新登录 先删除用户信息
                [UserInfo deleteUserInfo];
                //登录
                [WYPublic ifUserHaveLogin];
                [WYPublic showMessageWithTitle:@"提示：" Msg:@"登录信息已过期"];
            }else{
                [WYPublic showMessageWithTitle:@"错误提示:" Msg:dic[@"msg"]];
            }
            
        }
        
        [WYPublic removeZheZhaoToWindow];
    } failedBlock:^{
        [WYPublic removeZheZhaoToWindow];
    } needLog:NO];
}
//默认信息
- (void)MorenInfoWitnName:(NSString *)name Phone:(NSString *)phone Address:(NSString *)address Detail:(NSString *)detail Sheng:(NSString *)sheng Shi:(NSString*)shi Xian:(NSString *)xian
{
    nameText.text = name;
    phoneText.text = phone;
    addressLab.text = address;
    addressDetailTetx.text = detail;
    _name = name;
    _phone = phone;
    _detail = detail;
    
    _sheng = sheng;
    _shi = shi;
    _xian = xian;
}
@end
